package com.ruoyi.stock.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.stock.mapper.StockInventoryLogMapper;
import com.ruoyi.stock.domain.StockInventoryLog;
import com.ruoyi.stock.service.IStockInventoryLogService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;

/**
 * 库存变动日志Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-17
 */
@Service
public class StockInventoryLogServiceImpl implements IStockInventoryLogService 
{
    @Autowired
    private StockInventoryLogMapper stockInventoryLogMapper;

    /**
     * 查询库存变动日志
     * 
     * @param logId 库存变动日志主键
     * @return 库存变动日志
     */
    @Override
    public StockInventoryLog selectStockInventoryLogByLogId(Long logId)
    {
        return stockInventoryLogMapper.selectStockInventoryLogByLogId(logId);
    }

    /**
     * 查询库存变动日志列表
     * 
     * @param stockInventoryLog 库存变动日志
     * @return 库存变动日志
     */
    @Override
    public List<StockInventoryLog> selectStockInventoryLogList(StockInventoryLog stockInventoryLog)
    {
        return stockInventoryLogMapper.selectStockInventoryLogList(stockInventoryLog);
    }

    /**
     * 新增库存变动日志
     * 
     * @param stockInventoryLog 库存变动日志
     * @return 结果
     */
    @Override
    public int insertStockInventoryLog(StockInventoryLog stockInventoryLog)
    {
        if (stockInventoryLog.getOperateTime() == null)
        {
            stockInventoryLog.setOperateTime(DateUtils.getNowDate());
        }
        return stockInventoryLogMapper.insertStockInventoryLog(stockInventoryLog);
    }
    
    /**
     * 记录库存变动日志
     * 
     * @param productId 商品ID
     * @param productName 商品名称
     * @param beforeQuantity 变动前数量
     * @param afterQuantity 变动后数量
     * @param changeType 变动类型（1入库 2出库 3盘点）
     * @param relatedDocType 关联单据类型（1采购 2销售 3盘点）
     * @param relatedDocId 关联单据ID
     * @param warehouseCode 仓库编码
     * @param operator 操作人
     * @param remark 备注
     * @return 结果
     */
    @Override
    public int recordInventoryChange(Long productId, String productName, Long beforeQuantity, Long afterQuantity, 
            String changeType, String relatedDocType, Long relatedDocId, String warehouseCode, String operator, String remark)
    {
        StockInventoryLog log = new StockInventoryLog();
        log.setProductId(productId);
        log.setProductName(productName);
        log.setBeforeQuantity(beforeQuantity);
        log.setAfterQuantity(afterQuantity);
        log.setChangeQuantity(afterQuantity - beforeQuantity);
        log.setChangeType(changeType);
        log.setRelatedDocType(relatedDocType);
        log.setRelatedDocId(relatedDocId);
        log.setWarehouseCode(warehouseCode);
        log.setOperator(operator);
        log.setOperateTime(new Date());
        log.setRemark(remark);
        
        return insertStockInventoryLog(log);
    }

    /**
     * 修改库存变动日志
     * 
     * @param stockInventoryLog 库存变动日志
     * @return 结果
     */
    @Override
    public int updateStockInventoryLog(StockInventoryLog stockInventoryLog)
    {
        return stockInventoryLogMapper.updateStockInventoryLog(stockInventoryLog);
    }

    /**
     * 批量删除库存变动日志
     * 
     * @param logIds 需要删除的库存变动日志主键
     * @return 结果
     */
    @Override
    public int deleteStockInventoryLogByLogIds(String logIds)
    {
        return stockInventoryLogMapper.deleteStockInventoryLogByLogIds(Convert.toStrArray(logIds));
    }

    /**
     * 删除库存变动日志信息
     * 
     * @param logId 库存变动日志主键
     * @return 结果
     */
    @Override
    public int deleteStockInventoryLogByLogId(Long logId)
    {
        return stockInventoryLogMapper.deleteStockInventoryLogByLogId(logId);
    }
} 